/*
 * @Author: lsjweiyi 759209794@qq.com
 * @Date: 2024-03-31 19:58:09
 * @LastEditors: lsjweiyi 759209794@qq.com
 * @LastEditTime: 2024-04-06 18:58:34
 * @FilePath: \web-ssg\plugins\naive-ui.ts
 * @Description: naive-ui 插件,官方提供的
 */

import type { NuxtSSRContext } from "#app";
import { setup } from "@css-render/vue3-ssr";

export default defineNuxtPlugin((nuxtApp) => {
    if (process.server) {
        const { collect } = setup(nuxtApp.vueApp);
        const originalRenderMeta = nuxtApp.ssrContext?.renderMeta;
        nuxtApp.ssrContext = nuxtApp.ssrContext || ({} as NuxtSSRContext);
        nuxtApp.ssrContext.renderMeta = () => {
            if (!originalRenderMeta) {
                return {
                    headTags: collect(),
                };
            }
            const originalMeta = originalRenderMeta();
            if ("then" in originalMeta) {
                return originalMeta.then((resolvedOriginalMeta: any) => {
                    return {
                        ...resolvedOriginalMeta,
                        headTags: resolvedOriginalMeta["headTags"] + collect(),
                    };
                });
            } else {
                return {
                    ...originalMeta,
                    headTags: originalMeta["headTags"] + collect(),
                };
            }
        };
    }
});
